package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import com.itheima.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/PcMember")
public class MemberController {
    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;
    @Reference
    private SetmealService setmealService;
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = memberService.findMemberByPage(queryPageBean);

            return new Result(true,"success",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    //编辑表格数据回显
    @RequestMapping("/findMemberById")
    public Result findMemberById(Integer id){
        try{
            Member member = memberService.findMemberById(id);
            return new Result(true,"success",member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    //更新会员数据
    @RequestMapping("/editMember")
    public Result UpdateMemberById(@RequestBody Member member){
        try{
            memberService.editMember(member);
            return new Result(true,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member){
        try{
            member.setRegTime(new Date());
            memberService.add(member);
            return new Result(true,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    @RequestMapping("/deleteMemberById")
    public Result deleteMemberById(Integer id){
        try{
            memberService.deleteMemberById(id);
            return new Result(true,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    @RequestMapping("/exportMember2Excel")
    public Result exportMember2Excel(Integer[] ids, HttpServletRequest request, HttpServletResponse response){
        //获得模板文件对象
        try{
            //根据Id查会员对象
            List<Member> list=memberService.findMemberByIds(ids);

            String excelPath = request.getSession().getServletContext().getRealPath("template")+ File.separator+ "member_template.xlsx";
            //读取模板文件创建Excel表格对象
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(excelPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获得工作表


            for (int i = 0;i<list.size();i++) {
                XSSFRow  row = sheet.createRow((i+3));
                Member member = list.get(i);
               List< Order> orderList  = orderService.findOrderByMemberId(member.getId());
               if (orderList!=null&&orderList.size()>0){
                for (Order order : orderList) {
                    Setmeal setmeal = setmealService.findById(order.getSetmeal_id());
                    row.createCell(11).setCellValue(setmeal.getName());
                   String address = orderService.findAddressById(order.getId());
                    row.createCell(13).setCellValue(address);
                    List<CheckGroup> checkGroups = setmeal.getCheckGroups();
                    for (CheckGroup checkGroup : checkGroups) {
                        row.createCell(12).setCellValue(checkGroup.getName());
                    }
                }}


                String fileNumber = member.getFileNumber();
                XSSFCell cell = row.createCell(2);
                cell.setCellValue(fileNumber);//档案号
                String name = member.getName();
                row.createCell(3).setCellValue(name);//姓名
                String sex = member.getSex();
                row.createCell(4).setCellValue(sex);//性别
                String phoneNumber = member.getPhoneNumber();
                row.createCell(5).setCellValue(phoneNumber);//手机号
                Date birthday = member.getBirthday();
                String s = DateUtils.parseDate2String(birthday);
                row.createCell(6).setCellValue(s);//生日
                String email = member.getEmail();
                row.createCell(7).setCellValue(email);//邮箱
                String idCard = member.getIdCard();
                row.createCell(8).setCellValue(idCard);//身份证号
                Date regTime = member.getRegTime();
                String s1 = DateUtils.parseDate2String(regTime);
                row.createCell(9).setCellValue(s1);//注册时间
                String remark = member.getRemark();
                row.createCell(10).setCellValue(remark);//备注
            }
            //通过输出流进行文件下载
            ServletOutputStream outputStream = response.getOutputStream();
            response.setHeader("content-Disposition","attachment;filename=member_report_template.xlsx");
            workbook.write(outputStream);

            outputStream.flush();
            outputStream.close();
            workbook.close();
            return new Result(true,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }


}

